import curses
import subprocess
from safe import maind
import os

def main(stdscr):
    # 初始化颜色
    curses.start_color()
    curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)

    # 设置背景颜色
    stdscr.bkgd(' ', curses.color_pair(1))

    # 清屏
    stdscr.clear()

    # 选项列表
    options = ["Run Operating System", "Enter Safe Mode", "GRUB Bootloader", "Exit"]
    current_option = 0

    while True:
        stdscr.clear()
        height, width = stdscr.getmaxyx()

        # 显示选项
        for idx, option in enumerate(options):
            x = width // 2 - len(option) // 2
            y = height // 2 - len(options) // 2 + idx
            if idx == current_option:
                stdscr.attron(curses.A_REVERSE)
                stdscr.addstr(y, x, option)
                stdscr.attroff(curses.A_REVERSE)
            else:
                stdscr.addstr(y, x, option)

        key = stdscr.getch()

        if key == curses.KEY_UP and current_option > 0:
            current_option -= 1
        elif key == curses.KEY_DOWN and current_option < len(options) - 1:
            current_option += 1
        elif key == curses.KEY_ENTER or key in [10, 13]:
            if options[current_option] == "Run Operating System":
                curses.endwin()  # 退出curses模式
                subprocess.run(["python3", "src/main.py"])
                curses.wrapper(main)  # 重新进入curses模式
            elif options[current_option] == "Enter Safe Mode":
                curses.endwin()  # 退出curses模式
                maind()
                curses.wrapper(main)  # 重新进入curses模式
            elif options[current_option] == "GRUB Bootloader":
                curses.endwin()  # 退出curses模式
                subprocess.run(["python3", "GRUB.py"])
                curses.wrapper(main)  # 重新进入curses模式
            elif options[current_option] == "Exit":
                break
            stdscr.refresh()
            stdscr.getch()
            break

        stdscr.refresh()

curses.wrapper(main)